﻿using FangDuShi_Admin.DBUtility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FangDuShi_Admin.DAL
{
    public partial class Common
    {

        public int GetRecordCount(string table, string strWhere)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"DECLARE @sqlStr  VARCHAR(8000)
SET @sqlStr ='select count(*) from '+ @tbname +@where
exec ( @sqlStr )");


            SqlParameter[] parameters = {
                    new SqlParameter("@tbname", SqlDbType.VarChar, 255),
                       new SqlParameter("@where", SqlDbType.VarChar, 255)                  };
            parameters[0].Value = table;
            if (string.IsNullOrEmpty(strWhere))
            {
                parameters[1].Value = "";
            }
            else
            {
                parameters[1].Value = " where " + strWhere;
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="strWhere"></param>
        /// <param name="fOrderFiled"></param>
        /// <returns></returns>
        public DataSet GetListByPage(string table, string fieldskey, int PageSize, int PageIndex, string strWhere, string fOrderFiled, out int total, string FieldShow = "*")
        {

            SqlParameter[] parameters = {
                    new SqlParameter("@tbname", SqlDbType.VarChar, 255),
                    new SqlParameter("@FieldKey", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageCurrent", SqlDbType.Int),
                    new SqlParameter("@FieldShow", SqlDbType.VarChar,255),
                    new SqlParameter("@FieldOrder", SqlDbType.VarChar,255),
                    new SqlParameter("@Where", SqlDbType.VarChar,1000),
                    new SqlParameter("@PageCount",SqlDbType.Int)
                    };
            parameters[0].Value = table;
            parameters[1].Value = fieldskey;
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = FieldShow;
            parameters[5].Value = fOrderFiled;
            parameters[6].Value = strWhere;
            parameters[7].Direction = ParameterDirection.Output;
            DataSet ds = DbHelperSQL.RunProcedure("sp_PageView", parameters, "ds", 60 * 30);
            total = int.Parse(parameters[7].Value.ToString());
            return ds;
        }

    }
}
